<template>

  <div class="speech-container">

    <h2>java内置库生成</h2>

    <div class="input-section">
      <input class="my-input" v-model="text" placeholder="请输入文字"/>
      <button @click="synthesizeSpeech" class="convert-button">转换语音</button>
    </div>

    <p v-if="message">{{ message }}</p>

    <audio
        ref="audio"
        :src="audioUrl"
        controls
        class="audio-player"
    />

  </div>

</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      text: '',
      message: '',
      audioUrl: 'http://localhost:9000/SpeechRecognition2/voice/8'
    }
  },

  methods: {
    async synthesizeSpeech() {
      try {
        const response = await axios.post('http://localhost:9000/SpeechRecognition2/speech', null, {
          params: { text: this.text }
        });

        this.message = response.data;
        this.audioUrl = response.data.audioUrl; //获取语音URL
        this.$refs.audio.load(); //加载音频
      } catch (error) {
        console.error(error);
        this.message = '语音生成失败';
      }
    }
  }
}
</script>

<style>

.speech-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

h2 {
  font-size: 20px;
  margin-bottom: 10px;
}

.input-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.my-input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 3px;
  font-size: 14px;
}

.convert-button {
  padding: 10px 20px;
  background-color: #007BFF;
  color: #fff;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

.convert-button:hover {
  background-color: #0056b3;
}

.audio-player {
  margin-top: 10px;
  width: 100%;
}

</style>